package m.jclecai.com.fragment;


import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.activity.LotteryProtocolActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * 普通注册
 */
public class RegisterOneFragment extends BaseFragment {
    //推荐人的姓名
    @BindView(R.id.et_referrer_name)
    EditText et_referrer_name;
    //用户名
    @BindView(R.id.et_register_name)
    EditText et_register_name;
    //登录密码
    @BindView(R.id.et_loge_password)
    EditText et_loge_password;
    //手机号码
    @BindView(R.id.et_phone)
    EditText et_phone;
    //输入图形验证码
    @BindView(R.id.et_graph_code)
    EditText et_graph_code;
    //清除手机号码
    @BindView(R.id.iv_clear_phone)
    ImageView iv_clear_phone;
    //图形验证码
    @BindView(R.id.gf_graph_code)
    ImageView gf_graph_code;
    //投注协议
    @BindView(R.id.tv_pact)
    TextView tv_pact;
    //下一步按钮
    @BindView(R.id.text_bottom)
    TextView text_bottom;
    @BindView(R.id.llPwd)
    LinearLayout llPwd;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_register_item1;
    }

    @Override
    public void initView() {
        //首次获取下验证码
        getImgCode();
        Bundle arguments = getArguments();
        if (arguments != null) {
            String param = arguments.getString("param");
            if(!TextUtils.isEmpty(param)) {
                Map<String, String> map = FastOk.get(param, new TypeReference<Map<String, String>>() {
                });
                String user = map.get("user");
                String password = map.get("password");
                if (!TextUtils.isEmpty(user)) {
                    et_register_name.setText(user);
                    et_register_name.setEnabled(false);
                }
                if (!TextUtils.isEmpty(password)) {
                    llPwd.setVisibility(View.GONE);
                    et_loge_password.setText(password);
                }
            }
        }

    }

    //监听文本
    @OnTextChanged(R.id.et_phone)
    void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s.toString())) {
            iv_clear_phone.setVisibility(View.GONE);
        } else {
            iv_clear_phone.setVisibility(View.VISIBLE);
        }
    }

    //清空文本
    @OnClick(R.id.iv_clear_phone)
    void clearTxt() {
        et_phone.setText(null);
    }

    //图形验证码
    @OnClick(R.id.gf_graph_code)
    void getImgCode() {
        Glide.with(getActivity()).load(Constant_Url.REGISTER_RAND_URL)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                .skipMemoryCache(true).into(gf_graph_code);//跳过内存缓存
    }

    /**
     * 提交
     */
    @OnClick(R.id.text_bottom)
    void submitResult() {
        String et_referrer_name_rg = et_referrer_name.getText().toString().trim();//推荐人姓名
        String et_register_name_rg = et_register_name.getText().toString().trim();//用户名
        String et_loge_password_rg = et_loge_password.getText().toString().trim();//登录密码
        String et_phone_rg = et_phone.getText().toString().trim();//手机号
        String et_graph_code_rg = et_graph_code.getText().toString().trim();//图形验证码

        if (TextUtils.isEmpty(et_referrer_name_rg)) {
            MyToast.showToast(getActivity(), "请输入推荐人用户名或邀请码");
            return;
        }
        if (et_register_name.isEnabled()) {
            if (TextUtils.isEmpty(et_register_name_rg)) {
                MyToast.showToast(getActivity(), "请输入用户名");
                return;
            }
            if (et_register_name_rg.length() < 4 || et_register_name_rg.length() > 16) {
                MyToast.showToast(getActivity(), "用户名长度为4-16位字符!");
                return;
            }
        }
        if (llPwd.getVisibility() == View.VISIBLE) {
            if (TextUtils.isEmpty(et_loge_password_rg)) {
                MyToast.showToast(getActivity(), "请输入密码");
                return;
            }
            if (et_loge_password_rg.length() < 6 || et_loge_password_rg.length() > 20) {
                MyToast.showToast(getActivity(), "密码长度为6-20位字符!");
                return;
            }
        }
        if (TextUtils.isEmpty(et_graph_code_rg)) {
            MyToast.showToast(getActivity(), "请输入验证码");
            return;
        }
        register(et_referrer_name_rg, et_register_name_rg, et_loge_password_rg, et_phone_rg, et_graph_code_rg);
    }

    private void register(String et_referrer_name_rg, String et_register_name_rg, String et_loge_password_rg, String et_phone_rg, String et_graph_code_rg) {
        final HashMap<String, String> pair = new HashMap<>();
        pair.put("user", et_register_name_rg);//用户姓名
        pair.put("sessionid", et_graph_code_rg);//图形验证码
        pair.put("phone", et_phone_rg);//手机号码
        pair.put("parentName", et_referrer_name_rg);//推荐人姓名
        pair.put("password", et_loge_password_rg);//登录密码
        pair.put("channel", "android_"+ Build.VERSION.RELEASE+"_"+Build.MODEL);
        pair.put("appstore", ChannelUtil.getChannel());
        showDialog();
        HttpManager.postAsyn(Constant_Url.NEWLY_REGISTER_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(getActivity(), getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if (proofBean != null) {
                        MyToast.showToast(getActivity(), proofBean.getMessage());
                        //这里设置自动登录
                        if ("0".equals(proofBean.getStatus()))
                            logIn(pair.get("user"), pair.get("password"), new Callback() {
                                @Override
                                public void callbackSuccess() {
                                    super.callbackSuccess();
                                    getActivity().setResult(Activity.RESULT_OK);
                                    getActivity().finish();
                                }

                                @Override
                                public void callbackField() {
                                    super.callbackField();
                                    MyToast.showToast(getActivity(), "登录失败请尝试手动登录！");
                                }
                            });
                    } else {
                        MyToast.showToast(getActivity(), getString(R.string.error_network));
                    }
                }
            }
        }, pair);
    }

    @OnClick(R.id.tv_pact)
    void protocol() {
        startActivity(LotteryProtocolActivity.getIntent(getActivity()));
    }
}
